﻿using UnityEngine;
using UnityEngine.EventSystems;

namespace Gj
{
    public class MagnetHelper : NodeHelper
    {
        public bool invert;
        public KeyCode keyCode = KeyCode.None;
        private bool allow;

        protected override BindDataType bindDataType => BindDataType.Bool;

        private void Update()
        {
            if (keyCode == KeyCode.None) return;
            if (!allow) return;

#if UNITY_IPHONE || UNITY_ANDROID
#elif UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
            if (Input.GetKeyDown(keyCode)) Emit(UIEvent.Down);
            if (Input.GetKeyUp(keyCode)) Emit(UIEvent.Up);
#endif
        }

        protected override void BindData(bool b)
        {
            allow = invert ? !b : b;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (allow) Emit(UIEvent.Down);
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (allow) Emit(UIEvent.Up);
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (allow) Emit(UIEvent.Move);
        }
    }
}